package com.googlecode.c4e.ui.adapters;

import java.net.URL;
import java.util.List;

import org.apache.maven.continuum.xmlrpc.client.ContinuumXmlRpcClient;
import org.apache.maven.continuum.xmlrpc.project.ProjectGroupSummary;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.model.IWorkbenchAdapter;

import com.googlecode.c4e.model.Profile;
import com.googlecode.c4e.model.ProjectGroup;

public class ProfileAdapter implements IWorkbenchAdapter {

  public Object[] getChildren(Object obj) {
    try {
      Profile profile = (Profile) obj;
      ContinuumXmlRpcClient client = new ContinuumXmlRpcClient(new URL(profile.getConnectionUrl()), profile.getUsername(), profile.getPassword());
      List<ProjectGroupSummary> groups = client.getAllProjectGroups();
      ProjectGroup result[] = new ProjectGroup[groups.size()];
      for (int i = 0; i < groups.size(); i++) {
        result[i] = new ProjectGroup(groups.get(i), profile.getId());
      }
      return result;
    } catch (Exception cce) {
      cce.printStackTrace();
    }
    return new Object[0];
  }

  public ImageDescriptor getImageDescriptor(Object obj) {
    // TODO Auto-generated method stub
    return null;
  }

  public String getLabel(Object obj) {
    try {
      Profile profile = (Profile) obj;
      return profile.getLabel();
    } catch (ClassCastException cce) {
      return "CCE : " + obj.getClass().getName();
    }
  }

  public Object getParent(Object obj) {
    System.err.println("getParent" + getClass().getName());
    return null;
  }

}
